package com.nondo.dean.product.controller.api.app;

import com.nondo.dean.common.annotation.ApiVersion;

import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.product.domain.ProductWithBLOBs;
import com.nondo.dean.product.domain.Series;
import com.nondo.dean.product.model.result.ProductBeanResult;
import com.nondo.dean.product.service.ProductService;
import com.nondo.dean.product.service.SeriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author xiaofan
 * @Description:
 * @throws
 * @date 2018-04-1014:29
 * @Version 0.1
 */
@RestController("apiAppProductSeries")
@RequestMapping("/{v}/app/series")
public class SeriesController {

    @Autowired
    ProductService productService;

    @Autowired
    SeriesService seriesService;


    /**
     * 获取全部系列
     *
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Series>> serieslist(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Series> query) {
        View<Page<Series>> view = new View<Page<Series>>(20000, "获取数据成功");
        Series series = query.getData();
        Page<Series> page = new Page<Series>(query.getPage(), query.getLimit(), seriesService.count(series));
        List<Series> seriesList = seriesService.getList(query.getOffset(), query.getLimit(), series);
        page.setContent(seriesList);
        view.setData(page);
        return view;
    }


    /**
     *采购系列分类产品展示
     * @param request
     * @param response
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/product/list")
    public View<Page<ProductBeanResult>> seriesProductList(
            HttpServletRequest request, HttpServletResponse response, @RequestBody Query<ProductWithBLOBs> query) {
        View<Page<ProductBeanResult>> view = new View<Page<ProductBeanResult>>(20000, "获取列表成功");
        ProductWithBLOBs productWithBLOBs = query.getData();
        Page<ProductBeanResult> page = null;
        if(productWithBLOBs.getSeriesId() !=null && productWithBLOBs.getCategoryId() !=0){
            page = new Page<ProductBeanResult>(query.getPage(), query.getLimit(),productService.countSeriesProduct(productWithBLOBs));
        }else {
            page = new Page<ProductBeanResult>(query.getPage(), query.getLimit(), this.productService.countProduct(productWithBLOBs));
        }
        List<ProductBeanResult> results=seriesService.getSeriesProduct(productWithBLOBs.getSeriesId(),productWithBLOBs.getCategoryId(),query.getOffset(),query.getLimit());
        page.setContent(results);
        view.setData(page);
        return view;
    }


}
